package com.ohs.fo.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.ohs.bll.util.ConfigurationManager;

public class PictureAction extends AbstractAction {
	private String pic;
	
	public String getImage() throws ServletException, IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("image/jpeg");
		response.setHeader("Content-Disposition", "attachment; filename=\"" + pic + "\"");
		response.setHeader("Cache-Control", "private");
		String filePath = ConfigurationManager.getAsString(ConfigurationManager.CONFIG, "fileupload.path");
		File file = new File(filePath + pic);
		String path = "";
		if (file.exists()) {
			path = filePath + pic;		
		} else {
			path = filePath + "default.png";
		}
		InputStream inputStream = new FileInputStream(path);
		OutputStream output = response.getOutputStream();
		byte[] buffer = new byte[8192];
		int bytesRead;
		while ((bytesRead = inputStream.read(buffer)) >= 0) {
			output.write(buffer, 0, bytesRead);
		}
		inputStream.close();
		output.close();
		return null;
	}

	public String getPic() {
		return pic;
	}

	public void setPic(String pic) {
		this.pic = pic;
	}
	
}
